from gurobipy import *
import numpy as np
from traffic_engineering_class import *

class TrafficEngineer(object):
	#  all_interblock_paths : a 3D-array. all_interblock_paths[i][j] is an array of all paths from i to j
	def __init__(self, aurora_network, all_interblock_paths):
		self.aurora_network = aurora_network
		self.all_interblock_paths = all_interblock_paths
		return

	def get_filename_param(self):
		return "ecmp"

	# returns a string description of this class
	def get_string(self):
		return "ECMP"

	def traffic_engineering_class(self):
		return TrafficEngineeringClass.ECMP

	# traditionally will implement ECMP
	def compute_path_weights(self, logical_topology_adj_matrix):
		nblocks = self.aurora_network.get_num_blocks()
		routing_weights = {}
		for i in range(nblocks):
			for j in range(nblocks):
				if i != j:
					num_paths = 0.
					for path in self.all_interblock_paths[i][j]:
						path_capacity = 10E10
						path_len = len(path) - 1
						curr_node = path[0]
						for path_index in range(1, path_len, 1):
							next_node = path[path_index]
							link_capacity = min(self.aurora_network.get_link_capacity(curr_node), self.aurora_network.get_link_capacity(next_node))
							path_capacity = min(path_capacity, logical_topology_adj_matrix[i][j] * link_capacity)
							curr_node = next_node
						if path_capacity > 0:
							routing_weights[path] = 1.
							num_paths += 1
						else:
							routing_weights[path] = 0.
					for path in self.all_interblock_paths[i][j]:
						routing_weights[path] /= num_paths
		#rweights_sum = np.zeros((nblocks,nblocks))
		#for path in routing_weights:
		#	src = path[0]
		#	dst = path[-1]
		#	rweights_sum[src][dst] += routing_weights[path]
		#print rweights_sum
		return routing_weights